import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/common/get_department_tree_entity.dart';
import 'package:get/get.dart';
///树形-获取(行政)部门树
class DepartmentTreeController extends GetxController {
  /// 组织数据
  List<GetDepartmentTreeEntity> treeList = [];

  /// 选中的节点
  GetDepartmentTreeEntity selectedEntity = GetDepartmentTreeEntity();

  /// 初始化数据列表
  Future<void> onInitData(GetDepartmentTreeEntity selectedEntity) async {
    this.selectedEntity = selectedEntity;
    if (treeList.isEmpty) {
      // 请求接口
      final res = await HttpUtil().generateGetRequest<List<GetDepartmentTreeEntity>>(Interface.getBasicConfigurationDepartmentTree).execute();
      res.onSuccess((result) {
        if (result.data != null) {
          treeList = result.data!;
          update();
        }
      });
    }
  }

  /// 点击节点
  Future<void> clickOnNode(GetDepartmentTreeEntity treeEntity) async {
    treeEntity.isExpand = !treeEntity.isExpand!;
    selectedEntity = treeEntity;
    update();
  }
}
